<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\PaperBarChart;
use App\Admin\Repositories\PaperLibrary;
use App\Admin\RowActions\Copy;
use App\Models\User;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Widgets\Modal;

class PaperLibraryController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PaperLibrary(), function (Grid $grid) {
//            $grid->model()->setConstraints([
//                'paper_librarie_id' => $this->id,
//            ]);
            $grid->column('id')->sortable()->width('5%');
            $grid->column('paper_title')->width('20%');

//            $grid->column('publish_state', '发布')->switch()->help('发布后题目不可编辑，请谨慎操作')->width('10%');
//            $grid->column('publish_state', '发布')
//                ->if (function () {
//                    return $this->publish_state == 1;
//                })
//                ->display('查看')
//                ->switch()
//                ->help('发布后题目不可编辑，请谨慎操作')
//                ->width('10%');

            $grid->column('publish_state', '发布')
                ->help('发布后题目不可编辑，请谨慎操作')
                ->if (function () {
                    return $this->publish_state == '未发布';
                })
                ->switch()
                ->else()
                ->display('已发布')
                ->width('15%');

            $grid->column('answer_user_num')->width('10%');
            $grid->column('created_at')->width('20%');

            $grid->column('操作')->display(function () {
                $editPage = admin_base_path('paper_feedback?paper_librarie_id='.$this->id);
                $html = '';
                if ($this->publish_state == 0)
                $html .= "<a class='btn-sm btn-primary' href='paper_library/{$this->id}/edit' style='margin-right: 10px;'>
                    编辑
                         </a>";
                $html .= "
                         <a class='btn-sm btn-primary' href='{$editPage}' style='margin-right: 10px;'>
                            题目管理
                         </a>
                         <a class='btn-sm btn-primary' href='paperbar/{$this->id}' style='margin-right: 10px;'>
                            答题统计
                         </a>";
                return $html;

            })->width('30%');


            $grid->disableActions(); // 隐藏操作栏
//            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
//                $actions->disableView();
//                $actions->append(new Copy(User::class));
//                // 当前行的某个字段的数据
//                if ($actions->row->publish_state == 1) {
//                    $actions->disableEdit();
//                }
//
//                $editPage = admin_base_path('paper_feedback?paper_librarie_id='.$actions->row->id);
//                $actions->append("<a href='{$editPage}' style='margin-right: 10px;'><i class='fa fa-eye'></i>题目管理</a>");
//                $actions->prepend("<a href='paperbar/{$this->id}' style='margin-right: 10px;'><i class='fa fa-eye'></i>答题统计</a>");
//            });




        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PaperLibrary(), function (Show $show) {
            $show->field('id');
            $show->field('paper_title');
            $show->field('paper_remark');
            $show->field('region_id');
            $show->field('admin_id');
            $show->field('answer_user_num');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PaperLibrary(), function (Form $form) {
            $form->display('id');
            $form->text('paper_title');
            $form->text('paper_remark');

            $region = \App\Admin\Repositories\SystemRegion::getList();
            $form->select('region_id', '所属地区')->options($region)->default(0, true);

//            $form->text('answer_user_num');
            $form->display('created_at');
            $form->display('updated_at');
            $form->hidden('admin_id');
            $form->hidden('publish_state');

            $form->submitted(function ($form) {
                $form->admin_id = Admin::user()->id;
            });
            $form->disableViewCheck(true);
        });
    }
}
